/**
 * 增加提示道具
 */

import GameScene from "./GameScene";
import gameShareAdsVideo from "./SdkTools/gameShareAdsVideo";
import PlayData, { GameType } from "./SdkTools/PlayData";
import storage from "./SdkTools/storage";

/**
 * 添加提示道具
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class addTiShi extends cc.Component {

    @property(cc.Node)
    kuang: cc.Node = null

    // LIFE-CYCLE CALLBACKS:
    issharesucces = false

    onLoad() {
        this.showUi()
    }

    protected onEnable(): void {
        gameShareAdsVideo.Instance.bannerAds_Show()
        gameShareAdsVideo.Instance.chaPingAds_Show()

        if (PlayData.Instance.shareGameGetItem > 0 && cc.sys.platform == cc.sys.WECHAT_GAME) {
            var self = this
            //@ts-ignore
            window.sharecallback = () => {
                if (PlayData.Instance.isshare == !0) {
                    console.log('click share');
                    PlayData.Instance.shareGameGetItem--
                    PlayData.Instance.isshare = !1
                    gameShareAdsVideo.Instance.showToast('分享成功')
                    this.addTiShiItem()

                    //@ts-ignore
                    wx.offShow(window.sharecallback)
                } else {
                    gameShareAdsVideo.Instance.showToast('分享失败')
                    //@ts-ignore
                    window.clearTimeout(window.timeid)
                }
            }

            //@ts-ignore
            wx.onShow(window.sharecallback)
            cc.find('kuang/share', this.node).active = !0
            cc.find('kuang/pb', this.node).active = !1
        } else {
            cc.find('kuang/share', this.node).active = !1
            cc.find('kuang/pb', this.node).active = !0
        }
    }

    protected onDisable(): void {
        gameShareAdsVideo.Instance.bannerAds_Hide()
        cc.director.resume()

        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            //@ts-ignore
            if (window.sharecallback) {
                //@ts-ignore
                wx.offShow(window.sharecallback)
            }
        }
    }

    showUi() {
        this.kuang.scale = 0.3
        cc.tween(this.kuang)
            .to(0.1, { scale: 1 })
            .call(() => {
                cc.director.pause()
            })
            .start()
    }

    start() {

    }

    addBtnClick() {
        let self = this
        gameShareAdsVideo.Instance.motivational_Video_Show(() => {
            self.addTiShiItem()
        }, () => { })
    }

    addTiShiItem() {
        storage.Instance.setTiShi(storage.Instance.getTiShi() + 2)
        if (PlayData.Instance.gameType == GameType.finddifference_zhaobutong) {
            let sc2 = cc.find('Canvas').getComponent(GameScene)
            if (sc2) {
                sc2.setTiShi()
            }
        }
        this.closeBtnClick()
    }

    closeBtnClick() {
        this.node.destroy()
    }

    /**
     * 分享
     */
    shareGame() {
        if (cc.sys.platform == cc.sys.WECHAT_GAME) {
            gameShareAdsVideo.Instance.game_share(() => { })
            PlayData.Instance.isshare = !1
            //@ts-ignore
            window.timeid = window.setTimeout(() => {
                PlayData.Instance.isshare = !0
            }, 4000)
        } else {
            this.closeBtnClick()
        }


    }

    // update (dt) {}
}
